package com.hpin.fansea.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.CommunityListAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ChooseCommunity;
import com.hpin.fansea.bean.Community;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 游客选择小区页面
 * Created by Aaron on 15/9/10.
 */
public class CommunityListActivity extends BaseActivity {

    private ListView lv_community;
    private CommunityListAdapter communityListAdapter;
    private List<Community> communityArray = new ArrayList<Community>();

//    private String cityId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_community_select);

        mApplication.addActivity(this);

        initView();

        initData();

    }

    private void initView() {

//        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.community));

//        iv_left.setOnClickListener(this);

        lv_community = (ListView) findViewById(R.id.lv_community_first);
        communityListAdapter = new CommunityListAdapter(this, communityArray);
        lv_community.setAdapter(communityListAdapter);

        lv_community.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                String communityId = communityArray.get(arg2).projectId;
                String communityName = communityArray.get(arg2).projectName;
                Intent data = new Intent();
                data.putExtra("communityId", communityId);
                data.putExtra("communityName", communityName);
                setResult(20, data);
                finish();
            }
        });

    }

    /**
     * 获取小区数据
     */
    private void initData() {
        Bundle extras = getIntent().getExtras();
        String cityId = extras.getString("cityId");

//            DataService.communityList(this, handler, cityId);

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.HOUSESERVICE + cityId + Constant.PROJECTLIST,
                new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            ChooseCommunity chooseCommunity = JSONObject.parseObject(str, ChooseCommunity.class);

                            if (Constant.STATUS_SUCESS.equals(chooseCommunity.status)) {

                                communityArray= chooseCommunity.result;
                                communityListAdapter.setDate(communityArray);
                                communityListAdapter.notifyDataSetChanged();

                            }else {
                                showToast(chooseCommunity.message);
                            }

                        }catch (Exception e) {
                            e.printStackTrace();
                            showToast("获取小区信息失败");
                        }

                    }
                }, null);

    }

//    private Handler handler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            ResultEntity ro = (ResultEntity) msg.obj;
//            if (ro == null) {
//                dismissProgress();
//                // showToast("服务器异常");
//                return;
//            }
//            switch (msg.what) {
//                case MessageDefine.COMMUNITY_LIST:
//                    dismissProgress();
//                    if (ro.getStatus().equals("1000100030001")) {
//                        communityArray = JSONArray.parseArray(ro.getResult(),
//                                Community.class);
//                        communityListAdapter.setDate(communityArray);
//                        communityListAdapter.notifyDataSetChanged();
//                    } else {
//                        showToast(ro.getMessage());
//                    }
//                    break;
//
//                default:
//                    break;
//            }
//        }
//
//    };

    @Override
    public void widgetClick(View v) {

//        switch (v.getId()) {
//            case R.id.iv_left:
//                finish();
//                break;
//        }
    }
}
